package com.example.service.impl;

import com.example.entity.Movie;
import com.example.mapper.MovieMapper;
import com.example.service.MovieService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author: 15489  <br/>
 * Date: 2022/3/23:11:08  <br/>
 * Description:
 */
@Service
public class MovieServiceImpl implements MovieService {
    @Resource
    private MovieMapper movieMapper;

    @Transactional      //事务注解
    @Override
    public int addMovie(Movie movie) {
        return movieMapper.addMovie(movie);
    }
//    表示当i请安方法查询出来的结果可以放到redis中
    @Cacheable(value = "findById",key = "#id")
    public Movie findById(int id){
        System.out.println("......");
        return  movieMapper.findById(id);
    }

//    allEntries默认为false 不会删除数据
    @Override
//    @CacheEvict(value = "findById",allEntries = true)
    @Caching(evict = {
//            删除指定的key的数据
            @CacheEvict(value="findById",key = "#id")
    })
    @Transactional
    public int deById(int id) {
        return movieMapper.delById(id);
    }
}
